package org.cwnuit.train.acton;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Notices;
import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.service.NoticesService;
import org.cwnuit.train.service.TrainsService;
import org.cwnuit.train.service.UsersService;
import org.cwnuit.train.utils.MapUtil;
import org.cwnuit.train.utils.PageModel;
import org.cwnuit.train.utils.Today;

@SuppressWarnings("all")
public class NoticesAction extends HttpServlet{
	
	NoticesService noticesServices = new NoticesService();
	
	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		String cmd = request.getParameter("cmd");
		
	      if (cmd.equals("list")) {//分页列出信息
	    	request.setAttribute("content_tittle", "通知列表");
	    	request.setAttribute("pm", listNotice(request));
			request.getRequestDispatcher("web/notices/list.jsp").forward(request, response);
		 }else if(cmd.equals("adding")){
			 request.setAttribute("content_tittle", "通知编辑");
			 request.getRequestDispatcher("web/notices/input.jsp").forward(request, response);
		 }else if(cmd.equals("delete")){
			 if(deleteNotices(request)){
					request.setAttribute("msg", "恭喜你，你已经成功删除了一条通知的记录！");
				} else {
					request.setAttribute("msg", "对不起，你删除记录失败！");
				}
				request.getRequestDispatcher("notices?cmd=list").forward(request, response);
		 }else if(cmd.equals("updating")){
			 int nid = Integer.parseInt(request.getParameter("nid"));
				request.setAttribute("notice", findNotice(nid));
				request.setAttribute("content_tittle", "修改通知信息");
				request.getRequestDispatcher("web/notices/input.jsp").forward(request, response);
			 
		 }else if(cmd.equals("input"))
		 {
			 if (request.getParameter("nid") == null||request.getParameter("nid").equals("")) {
					if (addNotice(request)) {
						request.setAttribute("msg", "恭喜你，你已经成功添加了一条通知的记录！");
					} else {
						request.setAttribute("msg", "对不起，你添加记录失败！");
					}
				} else {
					if (updateNotice(request)) {
						request.setAttribute("msg", "恭喜你，你已经成功修改了一条通知的记录！");
					} else {
						request.setAttribute("msg", "对不起，你修改记录失败！");
					}
				}
				request.getRequestDispatcher("notices?cmd=list").forward(request, response);
		 } else  if (cmd.equals("list_watch")) {//分页列出信息
			request.setAttribute("content_tittle", "通知列表");
			request.setAttribute("pm", listNoticeWatch(request));
			request.getRequestDispatcher("web/notices/list_watch.jsp").forward(request, response);
		 } else  if (cmd.equals("watch")) {//查看公告
			int nid = Integer.parseInt(request.getParameter("nid"));
			request.setAttribute("notice", findNotice(nid));
			request.setAttribute("content_tittle", "查看公告");
			request.getRequestDispatcher("web/notices/detail.jsp").forward(request, response);
		}
	}
	
	private PageModel listNotice(HttpServletRequest request) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		String SQL = "SELECT * FROM Notices ORDER BY nid DESC";
		PageModel pm = noticesServices.findByPage(offset, 10, SQL);
		return pm;
	}
	
	private PageModel listNoticeWatch(HttpServletRequest request) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		int urole = ((Users)request.getSession().getAttribute("manager")).getUrole();
		String SQL = "SELECT * FROM Notices WHERE nrole like '%"+urole+"%' ORDER BY nid DESC";
		PageModel pm = noticesServices.findByPage(offset, 10, SQL);
		return pm;
	}
	
	private Notices findNotice(int nid) {
		String SQL = "SELECT * FROM notices WHERE nid = " + nid;
		return noticesServices.findNotice(SQL);
	}

	private boolean addNotice(HttpServletRequest request) {
		String ntitle = request.getParameter("ntitle");
		String ncontent = request.getParameter("ncontent"); 
		String[] nrole = request.getParameterValues("nrole");
		String nroleStr = "";
		for(int i = 0  ; i < nrole.length ; i++)  
		{  
			nroleStr += nrole[i];
		}
		String SQL = "INSERT INTO notices(ntitle,ncontent,nrole,ntime) VALUES('"
				+ ntitle + "','" + ncontent + "','"+nroleStr + "','"+new Today().getAllDateOfNum()+"')";
		return noticesServices.addNotice(SQL);
	}

	private boolean updateNotice(HttpServletRequest request) {
		int nid = Integer.parseInt(request.getParameter("nid"));
		String ntitle = request.getParameter("ntitle");
		String ncontent = request.getParameter("ncontent");
		String[] nrole = request.getParameterValues("nrole");
		String nroleStr = "";
		for(int i = 0  ; i < nrole.length ; i++)  
		{  
			nroleStr += nrole[i];
		}  

		String SQL = "UPDATE notices SET ntitle = '" + ntitle + "',ncontent = '"
				+ ncontent + "',nrole = '" + nroleStr + "' WHERE nid = " + nid;
		return noticesServices.updateNotice(SQL);
	}
	
	// 删除通知信息
	private boolean deleteNotices(HttpServletRequest request) {
		int nid = Integer.parseInt(request.getParameter("nid"));
		String SQL = "DELETE FROM Notices WHERE nid = " + nid;
		return noticesServices.deleteNotice(SQL);
	}
}
